import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import IResponse from '@/services/response.service';
import { ReqProvider } from '@/core/providers/req.provider';

@Injectable()
export class AuthService {
  constructor(
    private readonly jwtService: JwtService,
    private readonly reqProvider: ReqProvider,
  ) {}

  sign(id: number): string {
    return this.jwtService.sign({ id });
  }

  getUser() {
    const result = this.reqProvider.user
      ? {
          user: { ...this.reqProvider.user },
          ticket: this.sign(this.reqProvider.user.id),
        }
      : {
          user: null,
          ticket: '',
        };
    return IResponse.success(result);
  }
}
